package com.travel.manager.controller;

import com.github.pagehelper.PageInfo;
import com.travel.common.pojo.City;
import com.travel.common.util.JsonUtil;
import com.travel.manager.service.CityService;
import com.travel.manager.util.ExcelTest;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/city")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 城市列表页，页面使用easyUI的datagrid
     *
     * @param city 查询条件
     * @param page 页码，参数名不能变
     * @param rows 每页几条，参数名不能变
     * @return
     */
    @RequestMapping("/list.html")
    @ResponseBody
    public Map<String, Object> list(City city, Integer page, Integer rows) {
        PageInfo<City> pageInfo = cityService.getCityList(city, page, rows);
        Map<String, Object> result = new HashMap<>();
        //rows是datagrid需要的数据列表，这个key不能变
        result.put("rows", pageInfo.getList());
        //total是datagrid需要的数据总条数，这个key不能变
        result.put("total", pageInfo.getTotal());
        return result;
    }

    @RequestMapping("/edit.html")
    @ResponseBody
    public Map<String, Object> editCity(City city) {
        if (city.getId() == null) {//如果没有id代表新增
            cityService.addCity(city);
        } else {
            cityService.updateCity(city);
        }
        return JsonUtil.getOkMsg("编辑成功", null);
    }

    @RequestMapping("/export.html")
    public void all(HttpServletResponse response) throws Exception {
        List<City> cityList = cityService.getAllCityList();
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("学生表一");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("城市id");
        row.createCell(1).setCellValue("名称");
        int rowCount = 1;
        for (City city : cityList) {
            HSSFRow tmp = sheet.createRow(rowCount);
            tmp.createCell(0).setCellValue(city.getId());
            tmp.createCell(1).setCellValue(city.getName());
            rowCount++;
        }
        response.setContentType("application/excel;charset=utf=-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + "city-down.xls");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", -1);
        ServletOutputStream servletOutputStream = response.getOutputStream();
        wb.write(servletOutputStream);
        wb.close();
        servletOutputStream.flush();
        servletOutputStream.close();
    }

    @RequestMapping("/upload.html")
    @ResponseBody
    public Map<String, Object> uploadCity(MultipartFile file) throws Exception {
        List<City> cityList = ExcelTest.readCity(file);
        for (City city : cityList) {
            cityService.addCity(city);
        }
        return JsonUtil.getOkMsg("添加成功", null);
    }
}
